﻿--[[
   @brief    ui logic of toolbox
   @author   jasonlli
   @version  1.0
   @date     2015-03-16
--]]

tool_box_view = {
    tool_box_normal_size = {width = 812, height = 647};

    init = function()
        ierd_tgp.tpf_ui.Log("[toolbox] init.");
    end;


    close = function(uilib)
        if tool_box_model.rootId == 0 then
            return;
        end 
        local nToolBoxDlg = tool_box_model.rootId;
        -- 销毁阴影
        --shadow_helper.destroy_shadow(nToolBoxDlg);
        local pControl = uilib:GetControlPtr(nToolBoxDlg);
        local pToolBox = tolua.cast(pControl, "Tenio::ITDialog");
        pToolBox:Show(FALSE);
        layout_mgr.delay_remove_ctrl(nToolBoxDlg);

        tool_box_model.rootId = 0;
        tool_box_model.game_update_page_opened = false;
    end;

    open_toolbox_page = function(data_table)
        base_utility.log("[toolbox] start open wind");
        tool_box_view._restore_tpf();
        tool_box_view._arrange_window();
        tool_box_view._load_html(data_table);
    end;

    _restore_tpf = function()
        if tool_box_model.rootId ~= 0 then
            return;
        end

        tool_box_model.rootId = layout_mgr.restore_layout_multi("ToolBox.tpf");
    end;

    on_dialog_close = function(uilib)
        local param = {};
        event_center.send_event(tool_box_event.TOOL_BOX_CLOSE_PAGE, base_utility.table_to_json(param));
    end;

    _arrange_window = function()
        if tool_box_model.rootId == 0 then
            return;
        end

        local uilib = ui_utility.uilib();
        if uilib == nil then
            return;
        end

        base_utility.log("[toolbox]_arrange_window");
        local nToolBoxDlg = tool_box_model.rootId;
        local pControl = uilib:GetControlPtr(nToolBoxDlg);
        local pToolBoxDlg = tolua.cast(pControl, "Tenio::ITDialog"); 
        tool_box_view.dialog_ptr = pToolBoxDlg;
        --pToolBoxDlg:Restore();

        -- 将主界面显示出来
        if wegame_mainframe_view.is_closed == true then
            event_center.send_event(mainframe_event.SHOW_MAIN_WND, "");
        end;
        
        pToolBoxDlg:MoveToCenter();
        -- 不直接显示，收到页面消息i_service_show_wnd后再显示
        -- pToolBoxDlg:Show(TRUE);
        pToolBoxDlg:BringToTop();
        -- 页面在状态栏关闭时通知工具箱按钮更新状态。
        local on_dialog_close, str_holder1 = U8toCUIStr("tool_box_view.on_dialog_close");
        pControl:SetScriptFunction(Tenio.ESE_ON_DIALOG_CLOSE, on_dialog_close);
        -- 创建阴影
        -- if ui_utility.get_dpi_scale() == 1.0 then
            -- --shadow_helper.create_shadow(nToolBoxDlg);
        -- end;
    end;

    _load_html = function(data_table)
        

        local uilib = ui_utility.uilib();
        if uilib == nil then
            return;
        end

        base_utility.log("[toolbox]_load_html");
        local nQBWebID = uilib:GetControlIDbyCustomID(CID_TOOLBOX_QBWEB);
        local pControl = uilib:GetControlPtr(nQBWebID);
        local pQBWeb = tolua.cast(pControl, "Tenio::IQBWebBrowser");
        local tool_box_url = LoadStr("SOCIA_TOOL_BOX_URL");
        local url = tool_box_url..'?win_id=' .. tostring(tool_box_model.rootId) ..
            '&tab_id='.. tostring(data_table.tab_id);
        if data_table.game_id ~= nil then
            url = url .. '&game_id=' .. tostring(data_table.game_id);
        end

        --local logined_url = web_utility.make_login_status_url(url, login_model.main_uin);
        local const_ui_str, str_holder = U8toCUIStr(url);
        if nil ~= pQBWeb then
            pQBWeb:Navigate(const_ui_str);
        end
    end;

    ShowWaitUI = function(show_ui)
        local uilib = ui_utility.uilib();
        if uilib == nil then
            return;
        end

        base_utility.log("[toolbox]ShowWaitUI " .. ((show_ui and 'true') or 'false'));
        local dlg_id = uilib:GetControlIDbyCustomID(CID_TOOLBOX_DLG);
        local pWebView = ui_utility.get_ctrl_ptr_by_path(
            dlg_id, "QBWebBrowser_Web");

        if pWebView ~= nil then
            pWebView:Show(true);
        end
    end;

    OnLoadOk = function()
        base_utility.log("[toolbox]web notify Load ok");
        --tool_box_view.ShowWaitUI(false);
    end;

    OnNavigateOK = function(pUILib, sWebUrl, dwCtrlID)
        base_utility.log("[toolbox]OnNavigateOK");
    end;

    OnNavigateFail = function(pUILib, sWebUrl, dwCtrlID)
        base_utility.log("[toolbox]OnNavigateFail");
        --tool_box_view.ShowWaitUI(false);
        ui_utility.show_window(tool_box_model.rootId, true);
    end;

    OnNavigateCrash = function(pUILib, dwCtrlID)
        base_utility.log("[toolbox]OnNavigateCrash");
        --tool_box_view.ShowWaitUI(false);
        ui_utility.show_window(tool_box_model.rootId, true);
    end;

    _find_last  = function(haystack, needle)
        local i = haystack:match(".*"..needle.."()");
        if i == nil then 
            return nil;
        else 
            return i-1;
        end;
    end;

    on_hide = function()
        base_utility.log("[toolbox]on_hide");

        if tool_box_model.rootId == 0 then
            return;
        end 

        local id = tool_box_model.rootId;
        layout_mgr.delay_remove_ctrl(id);
        tool_box_model.rootId = 0;
    end;   
};
tool_box_view.init();